package com.shallwead.bna.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.shallwead.bna.BnA;
import com.shallwead.bna.object.AdBack;
import com.shallwead.bna.object.AdClick;
import com.shallwead.bna.object.AdFront;
import com.shallwead.bna.object.AdTracker;

public class JSON {

	public static AdFront getFrontAd() {
		AdFront adFront = null;
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(Constants.getFrontUrl + BnA.pkg);
		try {
			HttpResponse response = client.execute(request);
			InputStream content = response.getEntity().getContent();
			Header contentEncoding = response.getEntity().getContentEncoding();
			if (contentEncoding != null
					&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
				content = new GZIPInputStream(content);
			}
			StringBuilder json = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					content, "utf-8"));
			String line = reader.readLine();
			while (line != null) {
				json.append(line);
				line = reader.readLine();
			}

			Log.e(Constants.getFrontUrl, json.toString());

			JSONObject adJsonObject = new JSONObject(json.toString());
			JSONObject ad = adJsonObject.getJSONObject("ad");
			adFront = new AdFront(ad.getString("adIDX"), ad.getString("name"),
					ad.getString("url"), ad.getString("pkg"),
					ad.getString("linkType"), ad.getString("viewType"),
					ad.getString("imageUrl"), ad.getString("delay"));

			Log.e("adFront", adFront.toString());

		} catch (Exception e) {
			Log.e("DayTrader", "Exception getting JSON data", e);
		}

		return adFront;

	}

	public static ArrayList<AdBack> getBackAdList() {
		ArrayList<AdBack> adBackList = new ArrayList<AdBack>();
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(Constants.getBackUrl + BnA.pkg);
		try {
			HttpResponse response = client.execute(request);
			InputStream content = response.getEntity().getContent();
			Header contentEncoding = response.getEntity().getContentEncoding();
			if (contentEncoding != null
					&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
				content = new GZIPInputStream(content);
			}
			StringBuilder json = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					content, "utf-8"));
			String line = reader.readLine();
			while (line != null) {
				json.append(line);
				line = reader.readLine();
			}

			Log.e(Constants.getBackUrl, json.toString());

			JSONObject adJsonObject = new JSONObject(json.toString());
			JSONArray adJsonArray = new JSONArray(
					adJsonObject.getString("adList"));
			for (int i = 0; i < adJsonArray.length(); i++) {
				JSONObject ad = adJsonArray.getJSONObject(i);
				adBackList.add(new AdBack(ad.getString("adIDX"), ad
						.getString("name"), ad.getString("url"), ad
						.getString("pkg"), ad.getString("linkType"), ad
						.getString("viewType"), ad.getString("imageUrl")));

			}

		} catch (Exception e) {
			Log.e("DayTrader", "Exception getting JSON data", e);
		}

		return adBackList;

	}

	public static boolean sendAdClick(Context mContext, AdClick adClick) {

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(Constants.adClickUrl);
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("pkg", adClick.getPkg()));
			nameValuePairs.add(new BasicNameValuePair("adIDX", adClick
					.getAdIDX()));
			request.setEntity(new UrlEncodedFormEntity(nameValuePairs,
					HTTP.UTF_8));

			HttpResponse response = client.execute(request);
			InputStream content = response.getEntity().getContent();
			Header contentEncoding = response.getEntity().getContentEncoding();
			if (contentEncoding != null
					&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
				content = new GZIPInputStream(content);
			}
			StringBuilder json = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					content, "utf-8"));
			String line = reader.readLine();
			while (line != null) {
				json.append(line);
				line = reader.readLine();
			}
			Log.e(Constants.adClickUrl, json.toString());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static boolean sendAdInstall(Context mContext, AdTracker adTracker) {

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(Constants.adInstall);
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs
					.add(new BasicNameValuePair("pkg", adTracker.getPkg()));
			nameValuePairs.add(new BasicNameValuePair("adIDX", adTracker
					.getAdIDX()));
			nameValuePairs
					.add(new BasicNameValuePair("udid", adTracker.getU()));
			request.setEntity(new UrlEncodedFormEntity(nameValuePairs,
					HTTP.UTF_8));

			HttpResponse response = client.execute(request);
			InputStream content = response.getEntity().getContent();
			Header contentEncoding = response.getEntity().getContentEncoding();
			if (contentEncoding != null
					&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
				content = new GZIPInputStream(content);
			}
			StringBuilder json = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					content, "utf-8"));
			String line = reader.readLine();
			while (line != null) {
				json.append(line);
				line = reader.readLine();
			}
			Log.e(Constants.adInstall, json.toString());

			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					"");
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					"");

			return true;
		} catch (Exception e) {
			return false;
		}

	}

	public static boolean sendAdTracker(Context mContext, AdTracker adTracker) {

		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(Constants.trackerUrl + "?adIDX="
				+ adTracker.getAdIDX() + "&pkg=" + adTracker.getPkg()
				+ "&udid=" + adTracker.getU());
		try {

			HttpResponse response = client.execute(request);
			InputStream content = response.getEntity().getContent();
			Header contentEncoding = response.getEntity().getContentEncoding();
			if (contentEncoding != null
					&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
				content = new GZIPInputStream(content);
			}
			StringBuilder json = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					content, "utf-8"));
			String line = reader.readLine();
			while (line != null) {
				json.append(line);
				line = reader.readLine();
			}
			Log.e(Constants.trackerUrl, json.toString());
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	public static boolean sendPushClick(Context mContext, AdTracker adTracker) {

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(Constants.pushClick);
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("pushIDX", adTracker
					.getAdIDX()));
			nameValuePairs
					.add(new BasicNameValuePair("udid", adTracker.getU()));
			request.setEntity(new UrlEncodedFormEntity(nameValuePairs,
					HTTP.UTF_8));

			HttpResponse response = client.execute(request);
			InputStream content = response.getEntity().getContent();
			Header contentEncoding = response.getEntity().getContentEncoding();
			if (contentEncoding != null
					&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
				content = new GZIPInputStream(content);
			}
			StringBuilder json = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					content, "utf-8"));
			String line = reader.readLine();
			while (line != null) {
				json.append(line);
				line = reader.readLine();
			}
			Log.e(Constants.pushClick, json.toString());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static boolean sendPushInstall(Context mContext, AdTracker adTracker) {

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(Constants.pushInstall);
		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("pushIDX", adTracker
					.getAdIDX()));
			nameValuePairs
					.add(new BasicNameValuePair("udid", adTracker.getU()));
			request.setEntity(new UrlEncodedFormEntity(nameValuePairs,
					HTTP.UTF_8));

			HttpResponse response = client.execute(request);
			InputStream content = response.getEntity().getContent();
			Header contentEncoding = response.getEntity().getContentEncoding();
			if (contentEncoding != null
					&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
				content = new GZIPInputStream(content);
			}
			StringBuilder json = new StringBuilder();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					content, "utf-8"));
			String line = reader.readLine();
			while (line != null) {
				json.append(line);
				line = reader.readLine();
			}
			Log.e(Constants.pushInstall, json.toString());

			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					"");
			SharedPreferenceUtil.putStringSharedPreference(mContext, "pushidx",
					"");

			return true;
		} catch (Exception e) {
			return false;
		}

	}

}
